package jmine.tec.report.impl.table.pdf;

import java.awt.Color;

import jmine.tec.report.api.table.ReportCell;
import jmine.tec.report.impl.ReportBuilder;
import jmine.tec.utils.Tuple;

import com.lowagie.text.Chunk;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPTable;

/**
 * 
 * @author adalberto.kishi
 *
 */
public class ReportFooterCellPDFRenderer implements ReportCellPDFRenderer {

    private static final String DEFAULT_FONT_NAME = "Helvetica";

    private static final int FONT_SIZE_TITLE = 6;

    /**
     * {@inheritDoc}
     */
    public void createCell(PdfPTable table, ReportCell cell) {
        createCell(table, cell, Element.ALIGN_LEFT);
    }

    /**
     * {@inheritDoc}
     */
    public String getRendererType() {
        return ReportBuilder.TYPE_REPORT_FOOTER;
    }

    /**
     * {@inheritDoc}
     */
    public String getFormattedString(ReportCell cell) {
        return String.valueOf(cell.getValue());
    }

    /**
     * {@inheritDoc}
     */
    public void createCell(PdfPTable table, ReportCell cell, int horizontalAlignment) {
        Font font = FontFactory.getFont(DEFAULT_FONT_NAME, FONT_SIZE_TITLE, Font.NORMAL, Color.BLACK);
        Font fontBold = FontFactory.getFont(DEFAULT_FONT_NAME, FONT_SIZE_TITLE, Font.NORMAL, Color.BLACK);
        Tuple<?, ?> param = (Tuple<?, ?>) cell.getValue();
        
        Chunk chunkName = null;
        if (param.getHead() == null || param.getHead().toString().equals("")) {
            chunkName = new Chunk(param.getHead().toString(), fontBold);            
        } else {            
            chunkName = new Chunk(String.format("%s: ", param.getHead().toString()), fontBold);
        }
        
        Chunk chunkValue = new Chunk(param.getTail().toString(), font);
        if (param.getTail() == null || param.getTail().toString().equals("")) {
            chunkValue = new Chunk("", font);
        } else {
            chunkValue = new Chunk(param.getTail().toString(), font);
        }
        
        Phrase phrase = new Phrase(chunkName);
        phrase.add(chunkValue);
        table.getDefaultCell().setHorizontalAlignment(horizontalAlignment);
        table.addCell(phrase);
    }
    
}
